package ckf.man.practice.controller;

import ckf.man.practice.config.SchemeType;
import ckf.man.practice.entity.BaseData;
import ckf.man.practice.entity.DataStatus;
import ckf.man.practice.entity.Dto.BaseDataDto;
import ckf.man.practice.service.*;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/base_data")
public class BaseDataController {

    @Autowired
    private BaseDataService baseDataService;

    @Autowired
    private EquipmentService equipmentService;

    @Autowired
    private LateralLineService lateralLineService;

    @Autowired
    private SchemeNewService schemeNewService;

    @Autowired
    private DataStatusService dataStatusService;

    private List<BaseDataDto> transferDataDto(List<BaseData> results) {
        List<BaseDataDto> res = new ArrayList<>();
        for (BaseData record : results) {
            BaseDataDto dto = new BaseDataDto();
            BeanUtils.copyProperties(record, dto);
            int scheme = record.getSchemeId();
            String equipment = equipmentService.getById(record.getEquipmentId()).getEquipmentName();
            String laterLine = lateralLineService.getById(record.getLateralLineId()).getLateralLineName();
            String schemeNew = schemeNewService.getById(record.getSchemeNewId()).getSchemeNewContent();
            if (scheme == SchemeType.MATERIAL.getId()) {
                dto.setSchemeType(SchemeType.MATERIAL.getType());
            }

            if (scheme == SchemeType.PRODUCT.getId()) {
                dto.setSchemeType(SchemeType.PRODUCT.getType());
            }

            dto.setEquipment(equipment);
            dto.setLateralLine(laterLine);
            dto.setSchemeNew(schemeNew);
            res.add(dto);
        }

        return res;
    }


    @GetMapping("/get")
    public List<BaseDataDto> get() {
        List<BaseData> results = baseDataService.list();
        return transferDataDto(results);

    }

    @PostMapping("/getByEquipmentAndScheme")
    public List<BaseDataDto> getByEquipmentAndScheme(@RequestBody Map<String, Integer> data) {
        List<BaseData> results = baseDataService.getByEquipmentAndScheme(data.get("equipment_id"), data.get("scheme_id"));
        return transferDataDto(results);

    }

    @PostMapping("/getByCreateTime")
    public List<BaseDataDto> getByCreateTime(@RequestBody Map<String, Date> data) {
        List<BaseData> results = baseDataService.getByCreateTime(data.get("begin"), data.get("end"));
        return transferDataDto(results);
    }

    @PostMapping("/addOrUpdate")
    public String addOrUpdate(@RequestBody Map<String, List<BaseData>> data) {
        List<BaseData> records = data.get("list");
        boolean success = baseDataService.saveOrUpdateBatch(records);
        return success ? "修改成功" : "修改失败";
    }

    @PostMapping("/deleteByIds")
    public String delete(@RequestBody Map<String, List<Integer>> data) {
        List<Integer> ids = data.get("ids");
        for (int id : ids) {
            if (!dataStatusService.getByDataId(id).isEmpty()) {
                return "foreign key referencing, can not delete ";
            }
        }
        boolean success = baseDataService.removeBatchByIds(ids);
        return success ? "删除成功" : "删除失败";
    }
}
